var express = require('express');
var router = express.Router();
let sqlQuery = require('../mysqlmodel/sql')
var fs=require('fs');
let multer = require('multer');
// 配置上传对象
let upload = multer({dest:'../upload'});//上传文件的存储位置
/* GET home page. */
// 登录
router.post('/', async(req, res) =>{
  let id=req.body.id;
  let password=req.body.password;
   let sqlStr="select * from userlist where id=? and password=?";
  let result= await sqlQuery(sqlStr,[id,password]);
  if(result.length!=0){
    res.json({
    status:200
  })
  }else{
    res.json({
      status:400
    })
  }
 
  
});
// 注册
router.post('/register',async(req,res)=>{
  let id=req.body.id;
  let password=req.body.password;
  let sqlStr="insert into userlist (id,password) values (?,?)";
  await sqlQuery(sqlStr,[id,password])
  res.json({
    status:200
  })
})
// 获取用户列表
router.get('/users',async(req,res)=>{
 
  
  let pagesize=req.query.pagesize;
// console.log(pagesize)
  let pagenum=req.query.pagenum;
  let first=(pagenum-1)*pagesize;
  
    let  sqlStr="select * from userlist limit ?,?"
    let  result=await sqlQuery(sqlStr,[parseInt(first),parseInt(pagesize)]);

  let sqlS="select count(*) as num from userlist ";
  
 let resulti=await sqlQuery(sqlS);
 // console.log(resulti);
 // console.log(typeof(pagesize))//string类型的，要转化为number
  res.json({
   
    result,
    msg:"请求成功",
   resulti
  })
  
})
router.get("/search",async(req,res)=>{
  let userid=req.query.searchcontent;
  let sqlStr="select * from userlist where id =?"
  let result=await sqlQuery(sqlStr,[userid]);
  res.json(result);
})
// 删除
router.get("/del",async(req,res)=>{
  let userid=req.query.userid;
  let sqlStr = 'delete from userlist where id = ?';
  let result = await sqlQuery(sqlStr,[userid]);
  res.json({
    status:220,
    delcontent:result
})

})
// 编辑框中显示信息
router.get("/useredit",async(req,res)=>{
  let userid=req.query.uu;
  let sqlStr = 'select * from userlist where id = ?';
  let result = await sqlQuery(sqlStr,[userid]);
  res.json({
    status:220,
    result
})

})
// 编辑用户信息
router.post("/edituser",async(req,res)=>{
  let phone=req.body.phone;
  let email=req.body.email;
  let userid=req.body.userid;
  let sqlStr = 'update userlist set phone=?,email=?where id= ?';
  await sqlQuery(sqlStr,[phone,email,userid]);
  res.json({
    status:220,
   
})

})
// 添加用户
router.post("/adduser",async(req,res)=>{
  let phone=req.body.phone;
  let email=req.body.email;
  let userid=req.body.id;
  let password=req.body.password;
  let img=req.body.img;
  let sqlStr = 'insert into userlist (phone,email,id,password,img) values (?,?,?,?,?)';
  await sqlQuery(sqlStr,[phone,email,userid,password,img]);
  res.json({
    status:220,
})

})
// 添加商品
router.post("/addgoods",async(req,res)=>{

  let name=req.body.name;
  let price=req.body.price;
  let img=req.body.img;
  let sqlStr = 'insert into shopping (name,price,img) values (?,?,?)';
  await sqlQuery(sqlStr,[name,price,img]);
  res.json({
    status:220,
})

})
// 获取商品列表
router.get("/getgoodslist",async(req,res)=>{
  let sqlStr="select * from shopping"
  let result=await sqlQuery(sqlStr);
  res.json(result);
  
})
// 上传头像
router.post("/uploadimg",upload.single('file'), async(req,res)=>{
  let result=rename(req);
  res.json(result);
})
function rename(req){
  
  let oldPath = req.file.destination+"/"+req.file.filename;
  let newPath = req.file.destination+"/"+req.file.filename+req.file.originalname;
  fs.rename(oldPath,newPath,()=>{
    console.log("改名成功")
  })
  return {
    state:201,
    imgUrl:req.file.filename+req.file.originalname

  }
}
// 上传文章
router.post("/addarticle",async(req,res)=>{
 
  let title=req.body.title;
  let content=req.body.content;
  let author=req.body.author;
  let time=req.body.time;
  let authorimg=req.body.authorimg;
  let sqlStr="insert into articles (title,content,author,time,authorimg) values(?,?,?,?,?)"
  await sqlQuery(sqlStr,[title,content,author,time,authorimg]);
  res.json({
    state:201
  })
})
// 获取全部文章信息
router.get("/getarticle",async(req,res)=>{
  let sqlStr="select * from articles"
  let result=await sqlQuery(sqlStr);
  res.json(result);
})
// 获取作者头像
router.get("/getimg",async(req,res)=>{
  let id=req.query.xid;
  let sqlStr="select img from userlist where id=?"
  let result=await sqlQuery(sqlStr,[id]);
  res.json(result);
 
})
// 获取特定的文章
router.get("/getDetailarticle",async(req,res)=>{
  let id=req.query.id;
  let sqlStr="select * from articles where id=?"
  let result=await sqlQuery(sqlStr,[id]);
  res.json(result);
})
router.post("/upcomments",async(req,res)=>{
  let author=req.body.author;
  let user=req.body.user;
  let content=req.body.texta;
  let day=req.body.day;
  let img=req.body.img;
  let sqlStr="insert into comments(author,content,user,comtime,userimg) values (?,?,?,?,?)"
  await sqlQuery(sqlStr,[author,content,user,day,img]);
  res.json({
    state:201
  })
})
// 获得评论列表
router.get("/gettextlist",async(req,res)=>{
let author=req.query.xx;
  let sqlStr="select * from comments where author =?"
  let result=await sqlQuery(sqlStr,[author]);
  res.json(result);
})
// 删除文章
router.post("/delarticle",async(req,res)=>{
 let xid=req.body.yid;
 let sqlStr = 'delete from articles where id = ?';
 await sqlQuery(sqlStr,[xid]);
 res.json({
   state:201
})
})
// 更新文章
router.post("/changearticle",async(req,res)=>{
 
  let title=req.body.title;
  let content=req.body.content;
  let time=req.body.time;
  let editid=req.body.id;
  let sqlStr = 'update articles set title=?,content=? ,time=? where id= ?';
  await sqlQuery(sqlStr,[title,content,time,editid]);
  res.json({
    state:201,
})
})
// 获取标题
router.get("/getname",async(req,res)=>{
let title="%"+req.query.xname+"%";
  let sqlStr="select title,id from articles where title LIKE ?"
  let result=await sqlQuery(sqlStr,[title]);
  res.json(result);
})
// 加入购物车
router.post("/addcar",async(req,res)=>{
  let idnum=req.body.idnum;
  let iduser=req.body.iduser;
  let sqls="select * from shopping where id =?"
  let reslu=await sqlQuery(sqls,[idnum]);
  let name=reslu[0].name;
  let price=reslu[0].price;
  let img=reslu[0].img;
  let sql="select * from shoppingcar where idnum = ? and iduser =?"
  let resu=await sqlQuery(sql,[idnum,iduser]);
  //  console.log(resu);
  if(resu.length==0){
   
    let sqlStr = "insert into shoppingcar (idnum,name,price,img,iduser,goodsnum) values (?,?,?,?,?,?)";
     await sqlQuery(sqlStr,[idnum,name,price,img,iduser,1]);
  }else{
    let sqlStr = "update shoppingcar set goodsnum=? where idnum= ?";
    await sqlQuery(sqlStr,[resu[0].goodsnum+1,idnum]);
  }
 
  res.json({
    state:201
  })
})
// 获取购物车信息
router.get("/getcar",async(req,res)=>{
  let iduser=req.query.iduser;
  // let sqlS="select count(*) as num from shoppingcar ";
  // let resultnum=await sqlQuery(sqlS);
  let sqlSte="select * from shoppingcar where iduser =?"
  let result=await sqlQuery(sqlSte,[iduser]);
  res.json({
    
    result
  });
})
// 编辑购物车
router.post("/addcaragain",async(req,res)=>{
  let idnum=req.body.idnum;
  let iduser=req.body.iduser;
  //  console.log(resu);
  let sql="select * from shoppingcar where idnum = ? and iduser =?"
  let resu=await sqlQuery(sql,[idnum,iduser]);
    let sqlStr = "update shoppingcar set goodsnum=? where idnum= ? and iduser =?";
    await sqlQuery(sqlStr,[resu[0].goodsnum+1,idnum,iduser]);
  res.json({
    state:201
  })
})
// 删除商品
router.post("/delcar",async(req,res)=>{
  let idnum=req.body.idnum;
  let iduser=req.body.iduser;
  //  console.log(resu);
  let sql="select * from shoppingcar where idnum = ? and iduser =?"
  let resu=await sqlQuery(sql,[idnum,iduser]);
    let sqlStr = "update shoppingcar set goodsnum=? where idnum= ? and iduser =?";
    await sqlQuery(sqlStr,[resu[0].goodsnum-1,idnum,iduser]);
  res.json({
    state:201
  })
})
router.post("/delallcar",async(req,res)=>{
  let idnum=req.body.idnum;
  let iduser=req.body.iduser;
  //  console.log(resu);
  
    let sqlStr = "delete  from shoppingcar where idnum = ? and iduser =?";
    await sqlQuery(sqlStr,[idnum,iduser]);
  res.json({
    state:201
  })
})
module.exports = router;
